<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%--
- Author(s): 57853
- Date: 2018-05-21 17:27:01
- Description:
    --%>
    <head>
        <title>
            模板录入
        </title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <script src="<%= request.getContextPath() %>/common/nui/nui.js" type="text/javascript">
        </script>
    </head>
    <body>
        <!-- 标识页面是查看(query)、修改(edit)、新增(add) -->
        <input name="pageType" class="nui-hidden"/>
        <div id="dataform1" style="padding-top:5px;">
            <!-- hidden域 -->
            <input class="nui-hidden" name="zhmyfiletemplate.ftAddress" id="ftAddress" />
            <input class="nui-hidden" name="zhmyfiletemplate.ftId"/>
            <table style="width:100%;height:100%;table-layout:fixed;" class="nui-form-table">
                <tr>
                    <td class="form_label">
                        模板名称:
                    </td>
                    <td colspan="1">
                        <input class="nui-textbox" name="zhmyfiletemplate.ftName" required="true"/>
                    </td>
                 </tr>
                 <tr>
                    <td class="form_label">
                        所属进度:
                    </td>
                    <td colspan="1">
                        <input class="nui-dictcombobox" dictTypeId="PROJECT_PROGRESS" name="zhmyfiletemplate.ftProjectStatus" required="true" />
                    </td>
                </tr>
                <tr>
                	<td class="form_label">
                        上传文件:
                    </td>
                    <td colspan="1">
                    	<form id="fileForm" enctype="multipart/form-data" method="post">
     						<input type="file" name="file" id="file"/> 
    						<input type="button" value="上传" onclick="upload()" />     
  						</form>
                    </td>
                </tr>
            </table>
            <div class="nui-toolbar" style="padding:0px;" borderStyle="border:0;">
                <table width="100%">
                    <tr>
                        <td style="text-align:center;" colspan="4">
                            <a class="nui-button" iconCls="icon-save" onclick="onOk()">
                                保存
                            </a>
                            <span style="display:inline-block;width:25px;">
                            </span>
                            <a class="nui-button" iconCls="icon-cancel" onclick="onCancel()">
                                取消
                            </a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <script type="text/javascript">
            nui.parse();

            function saveData(){

                var form = new nui.Form("#dataform1");
                form.setChanged(false);
                //保存
                var urlStr = "com.zhmy.xmgl.zhmyfiletemplatebiz.addZhmyFileTemplate.biz.ext";
                var pageType = nui.getbyName("pageType").getValue();//获取当前页面是编辑还是新增状态
                //编辑
                if(pageType=="edit"){
                    urlStr = "com.zhmy.xmgl.zhmyfiletemplatebiz.updateZhmyFileTemplate.biz.ext";
                }
                form.validate();
                if(form.isValid()==false) return;

                var data = form.getData(false,true);
                var ftAddress = $("#ftAddress").val();
                data.zhmyfiletemplate.ftAddress = ftAddress;
                var json = nui.encode(data);
                $.ajax({
                    url:urlStr,
                    type:'POST',
                    data:json,
                    cache:false,
                    contentType:'text/json',
                    success:function(text){
                        var returnJson = nui.decode(text);
                        if(returnJson.exception == null){
                            CloseWindow("saveSuccess");
                        }else{
                            nui.alert("保存失败", "系统提示", function(action){
                                if(action == "ok" || action == "close"){
                                    //CloseWindow("saveFailed");
                                }
                                });
                            }
                        }
                        });
                    }

                    //页面间传输json数据
                    function setFormData(data){
                        //跨页面传递的数据对象，克隆后才可以安全使用
                        var infos = nui.clone(data);

                        //保存list页面传递过来的页面类型：add表示新增、edit表示编辑
                        nui.getbyName("pageType").setValue(infos.pageType);

                        //如果是点击编辑类型页面
                        if (infos.pageType == "edit") {
                            var json = infos.record;

                            var form = new nui.Form("#dataform1");//将普通form转为nui的form
                            form.setData(json);
                            form.setChanged(false);
                        }
                    }

                    //关闭窗口
                    function CloseWindow(action) {
                        if (action == "close" && form.isChanged()) {
                            if (confirm("数据被修改了，是否先保存？")) {
                                saveData();
                            }
                        }
                        if (window.CloseOwnerWindow)
                        return window.CloseOwnerWindow(action);
                        else window.close();
                    }

                    //确定保存或更新
                    function onOk() {
                        saveData();
                    }

                    //取消
                    function onCancel() {
                        CloseWindow("cancel");
                    }
                    
                    //异步文件上传
                    function upload() {
					    var formData = new FormData;
					    var fileData = $("#file");
						formData.append("myfile", fileData[0].files[0]);
					    var url = "<%=request.getContextPath()%>/xmgl/doSavefile1.jsp";
					    formData.projectId = " ";
					    $.ajax({    
					        contentType:"multipart/form-data",  
					        url:url,    
					        type:"POST",
					        data:formData,  
					        dataType:"text",  
					        processData: false,  // 告诉jQuery不要去处理发送的数据  
					        contentType: false,   // 告诉jQuery不要去设置Content-Type请求头  
					        success: function(data){
					         nui.alert("上传成功");
					        	var strcookie = document.cookie;
					        	var arrcookie  = strcookie.split("; ");
					        	for ( var i = 0; i < arrcookie.length; i++) {
									var arr = arrcookie[i].split("=");
									if (arr[0] == "address"){
										var address = $.trim(arr[1]);
										address=decodeURIComponent(address,"utf-8");
										var reg = /^[\'\"]+|[\'\"]+$/g;
										address = address.replace(reg,"");
									    
										$("#ftAddress").val(address);
									}
								}
					        	
					        }  
					    });  
                    }
                </script>
            </body>
        </html>
